package http

import (
	"net/http"

	"bilibi.com/comment/conf"
	"bilibi.com/comment/service"
	bm "github.com/bilibili/kratos/pkg/net/http/blademaster"
	log "github.com/sirupsen/logrus"
)

const _maxnamelen = 18

var (
	srv *service.Service
	cf  *conf.Config
)

// Init init
func Init(c *conf.Config, svc *service.Service) {
	srv = svc
	cf = c
	engine := bm.DefaultServer(c.BM)
	route(engine)
	if err := engine.Start(); err != nil {
		log.Error("bm Start error(%v)", err)
		panic(err)
	}
}

func route(e *bm.Engine) {
	e.Ping(ping)
	group := e.Group("/comment")
	group.POST("/add", addComment)
	group.GET("/query", queryComments)
	group.GET("/list", listComments)
	group.GET("/get", comment)
	group.GET("/get/byid", comment)
	group.GET("/get/byname", comment)
	group.POST("/modify", updateComment)
	group.POST("/delete", deleteComment)

}

func ping(c *bm.Context) {
	if err := srv.Ping(c); err != nil {
		log.Error("ping error(%v)", err)
		c.AbortWithStatus(http.StatusServiceUnavailable)
	}
}
